#ifndef _RCU_STRUCT_H_
#define _RCU_STRUCT_H_

//#define PACKED __attribute__ ((__packed__))
//#define min(a, b)  (((a) < (b)) ? (a) : (b))
//#define max(a, b)  (((a) > (b)) ? (a) : (b))
#define DEFAULT_SERVER_IP	"183.98.119.157"
#define UDP_PORT	12002
#define PACKET_SIZE	2048
#define NET_ADDRESS_LENGTH	16

typedef signed int int32;
typedef unsigned int int32u;

typedef signed short int16;
typedef unsigned short int16u;

typedef signed char int8;
typedef unsigned char int8u;


typedef struct {
	int32 seconds;
	int32 nanoseconds;
} PACKED TIME_INTERNAL_;


typedef struct {
	int32 sock;

	int8 raddr_temp[NET_ADDRESS_LENGTH];
	int32u rport_temp;

	int8u obuf[PACKET_SIZE];
	int8u ibuf[PACKET_SIZE];

	int32u ibuf_len;
} PACKED NETWORK_;



typedef struct {
	int8u start[4]; 		//$KTF
	int8u command;  		//KTC_STATUS_REPORT, ...
	int8u nickname;			//0x1F
	int8u version[2];		//decimal mms:10, rcu:??
	int8u master_id[10]; 	// maybe ascii , will be setted by GUI program
	int8u equip_no[8];		// rsvd, 0x80,0x80,0x80,0x80, 0x80,0x80,0x80,0x80
	int16u sequence;		// seq. #
	int16u	len;
} PACKED IPC_Header_T; // for rcu-g



typedef struct {
	IPC_Header_T	ipc;
//	int8u 	buf[1024];
	int8u 	buf[2048];			//kay
	int16u 	*p_crc;
	int8u 	*p_endflag;		// 2 bytes, $X ,[2];
	int16u 	crc_caculated;
	int16u 	valid;
} PACKED IPC_Data_T; // for rcu-g

typedef struct {
	IPC_Data_T	in;
	IPC_Data_T	out;
} PACKED IPCMSG_;

typedef struct {
	NETWORK_			net;
	IPCMSG_				msg;
} PACKED UDP_DAEMON_;

///////////////////////////////////////////////////
///////////////////////////////////////////////////
typedef struct{
	int32 type; //1: 8bit 2:16bit 4:32bit 0:end of array
	int32 size; //type*size= total size of data
}size_info;

typedef struct{
	int8 cmd; //KTC_STATUS_REPORT..
	size_info *p; //type*size= total size of data
	char *title;
}size_info_by_command;


//union BAG_O_BITS {
//    struct {
//        unsigned bit_000:1;
//        unsigned bit_001:1;
//        /* 316 more ... */
//        unsigned bit_318:1;
//        unsigned bit_319:1;
//    } bits;
//    unsigned char bytes[40];
//} bob;


// KTC_ALARM_REPORT
typedef struct {
	int8u ocxo_fault:1;
	int8u clk_na:1;
	int8u ant_open:1;
	int8u ant_short:1;

	int8u gps_holdover:1;
	int8u ocxo_efc:1;
	int8u rsvd1:1;
	int8u rsvd2:1;
} st_alarm_old;

typedef struct {
	int8u rsvd2:1;
	int8u rsvd1:1;
	int8u ocxo_efc:1;
	int8u gps_holdover:1;

	int8u ant_short:1;
	int8u ant_open:1;
	int8u clk_na:1;
	int8u ocxo_fault:1;

} st_alarm;

// KTC_ALARM_REPORT
typedef union{
	st_alarm b;
	int8u c;
} st_alarm_alm;


typedef struct {
	st_alarm_alm event;
	int16 rsvd1;
	st_alarm_alm value;
	int16 rsvd2;
} PACKED KTI_SubData_Alm_T; // for rcu-g

// KTC_STATUS_REPORT
typedef struct {
	int8 flag; 		// 0:period, 1:event
	int8 supplier1;	// 0x50
	int8 supplier2; // 0x50
	int16 rsvd1; //

	KTI_SubData_Alm_T alarm;
	int8 id[10];
	int16u period;
	int8 sn[15];
	int8 time_valid;
	int8 time_mode; //1:gps, 2:utc

	int32u 	leap_sec;
	int32u 	running_sec;
	int32u 	holdover_sec;
	int8 	status; // 1:warmup 2:lock
	int32u 	dac;
	int32 	offset; // in ns
	int32 	freq; //1e-12

	int8 	gps_visible;
	int8 	gps_tracking;
	int32u 	gps_dop;
	int8	gps_cn_mean;
	int8	gps_cn_max;
	int8	gps_cn_min;
	int8 	gps_ant; //1:good 2:short 3:open
	int8 	temperature;
	int8 	rsvd2;
	int8 	rsvd3[8];

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_Report_T_1st; // for rcu-g

// KTC_STATUS_REPORT
typedef struct {
	int8 flag; 		// 0:period, 1:event
	int8 supplier1;	// 0x50
	int8 supplier2; // 0x50
	int16 rsvd1; //

	KTI_SubData_Alm_T alarm;
	int8 id[10];
	int16u period;
	int8 sn[15];
	int8 time_valid;

	int32u 	running_sec;
	int32u 	holdover_sec;

	int8 	status; // 1:warmup 2:lock
	int32u 	dac;
	int8	freq[8];

	int8 	gps_visible;
	int8 	gps_tracking;
	int8	gps_cn_mean;
	int8	gps_cn_max;
	int8	gps_cn_min;

	int8 	gps_ant; //1:good 2:short 3:open
	int8 	temperature;
	int8 	RcvComm;
	int8 	rsvd3[8];

} PACKED KTI_SubData_Report_T; // for rcu-g

// KTC_Alarm_REPORT
typedef struct {
	KTI_SubData_Alm_T alarm;

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_Alarm_T; // for rcu-g

// KTC_CONTRL_REQ
typedef struct {
	int8 	event;
	int8	rsvd1[10];
	int16	period;
	int8	rsvd2[10];

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_CtrlReq_T; // for rcu-g

// KTC_IP_CHANGE_REQ
typedef struct {
	int8u 	ip[4];
	int16u	port;
	int16	rsvd;

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_IpReq_T; // for rcu-g

// KTC_IP_CHANGE_RSP
typedef struct {

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_IpRsp_T; // for rcu-g

// KTC_MAC_ADDR_RSP
typedef struct {
	int8u	mac[6];
	int8u	flag;
	int16u	rsvd;
	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_MacRsp_T; // for rcu-g

// KTC_MAC_ADDR_REQ
typedef struct {

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_MacReq_T; // for rcu-g

// KTC_RESET_REQ
typedef struct {

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_ResetReq_T; // for rcu-g

// KTC_RESET_RSP
typedef struct {

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_ResetRsp_T; // for rcu-g


// KTC_STATUS_REQ
typedef struct {

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_StatusReq_T; // for rcu-g

// KTC_DL_CMD_REQ
typedef struct {
	int8u version[2];
	int8 supplier1;	// 0x50
	int8 supplier2; // 0x50
	int16u 	total_frame;
	int16u	rsvd;

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_DL_CmdReq_T; // for rcu-g

// KTC_DL_CMD_RSP
typedef struct {
	int8u version[2];
	int8 supplier1;	// 0x50
	int8 supplier2; // 0x50
	int16u 	total_frame;
	int8u 	ready;
	int8u	rsvd;

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_DL_CmdRsp_T; // for rcu-g


// KTC_DL_DATA
typedef struct {
	int16u 	total_frame;
	int16u 	current_frame;

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_DL_Data_T; // for rcu-g


// KTC_DL_DATA_RSP
typedef struct {
	int16u 	total_frame;
	int16u 	current_frame;

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_DL_DataRsp_T; // for rcu-g

// KTC_DL_COMPLETE_REQ
typedef struct {
	int8u version[2];
	int8 supplier1;	// 0x50
	int8 supplier2; // 0x50
	int16u 	total_frame;
	int16u 	total_crc;

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_DL_CompleteReq_T; // for rcu-g

// KTC_DL_COMPLETE_RSP
typedef struct {
	int8 supplier1;	// 0x50
	int8 supplier2; // 0x50
	int16u 	total_crc;
	int8 result;
	int8u rsvd[3];

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_DL_CompleteRsp_T; // for rcu-g



// KTC_LOCAL_REQ
typedef struct {

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_LocalReq_T; // for rcu-g

// KTC_LOCAL_RSP
typedef struct {

	//int16 	crc;
	//int8	endflag[2];
} PACKED KTI_SubData_LocalRsp_T; // for rcu-g


/////////////////////////
/////////////////////////
typedef struct {
	int8u flag;
	int8u period;
} PACKED KTI_SubData_HeartbeatReq_T; // for rcu-g

typedef struct {
	int8u period;
} PACKED KTI_SubData_HeartbeatRsp_T; // for rcu-g


extern IPC_Header_T				buf_ipc;
extern KTI_SubData_Report_T 	buf_report;
extern KTI_SubData_Alm_T 		buf_alm,buf_alm_saved;
void SetUdpMsg();
///////////////////////////////////////////////////

typedef struct{
	int bReset;

}rcu_exec_flag_;

extern rcu_exec_flag_ rcuExecflag;

#include "msg.h"
#include "crc_kti.h"
///////////////////////////////////////////////////

#endif
